<template>
  <!-- 用户查看快递 -->
  <layout-box one="我的快递" two="查询快递">
    <el-row :gutter="20">
      <el-col :span="8">
        <el-input v-model="queryInfo.query" placeholder="请输入内容" clearable>
          <el-button icon="el-icon-search"></el-button>
        </el-input>
      </el-col>
      <el-col :span="4">
        <el-button type="primary" icon="el-icon-search" @click="getOrderList">查询</el-button>
      </el-col>
    </el-row>
    <el-table :data="orderList" border stripe>
      <el-table-column type="index"></el-table-column>
      <el-table-column label="寄件人姓名" prop="fromName"></el-table-column>
      <el-table-column label="寄件人地址" prop="fromCity"></el-table-column>
      <el-table-column label="收件人姓名" prop="toName"></el-table-column>
      <el-table-column label="收件人地址" prop="toCity"></el-table-column>
      <el-table-column label="物流信息" prop="currentArea">
        <template slot-scope="scope">
          <span v-if="scope.row.currentArea == null">等待物流更新</span>
          <span v-else>{{ scope.row.currentArea }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" prop="createTime">
        <template slot-scope="scope">{{
          scope.row.createTime | formatDate
        }}</template>
      </el-table-column>
      <el-table-column label="状态" prop="status">
        <template slot-scope="scope">
          <span v-if="scope.row.status == 0">已揽收</span>
          <span v-if="scope.row.status == 1">运输中</span>
          <span v-if="scope.row.status == 2">已签收</span>
          <span v-if="scope.row.status == 3">已到达驿站</span>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页区 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryInfo.pagenum"
      :page-sizes="[5, 10, 20, 50]"
      :page-size="queryInfo.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="orderList.length"
    ></el-pagination>
    <!-- 物流进度对话框 -->
    <el-dialog
      title="物流进度"
      :visible.sync="showProgressVisible"
      width="50%"
      @close="handleClose"
    >
      <el-form
        :model="progressInfo"
        :rules="addressFormRules"
        ref="addressFormRef"
        label-width="100px"
      >
        <el-form-item label="详细地址" prop="context">
          <el-input v-model="progressInfo.context"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addressVisible = false">取 消</el-button>
        <el-button type="primary" @click="addressVisible = false"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </layout-box>
</template>

<script>
export default {
  data() {
    return {
      queryInfo: {
        query: "",
        pagenum: 1,
        pagesize: 10,
      },
      total: 0,
      orderList: [],
      addressVisible: false,
      //   默认物流对话框是关闭状态的
      showProgressVisible: false,
      addressForm: {
        address1: [],
        address2: "",
      },
      addressFormRules: {
        address1: [
          { required: true, message: "请输入省市区/县", trigger: "blur" },
        ],
        address2: [
          {
            required: true,
            message: "请输入详情地址",
            trigger: "blur",
          },
        ],
      },
      //   物流信息
      progressInfo: [],
      express: {
        currentArea: "",
        fileType: 0,
        flowId: 0,
        fromAddress: "",
        fromArea: "",
        fromCity: "",
        fromName: "",
        fromPhone: "",
        fromProvince: "",
        postMan: "",
        status: 0,
        toAddress: "",
        toArea: "",
        toCity: "",
        toName: "",
        toPhone: "",
        toProvince: "",
        userId: "",
      },
    };
  },
  created() {
    this.getOrderList();
  },
  methods: {
    async expressEntry() {
      const { data: res } = await this.$http.post("/express/add", {
        express: this.express,
      });
    },
    // 查询所有
    async getOrderList() {
      if (window.sessionStorage.getItem("personInfo") != 1) {
        const { data: res } = await this.$http.get("/express/getAll");
        this.orderList = res.data;
        console.log(res);
      } else {
        //查询自己
        const { data: res } = await this.$http.post("/express/getByUserName", {
          query: this.queryInfo.query,
          pagenum: this.queryInfo.pagenum,
          pagesize: this.queryInfo.pagesize,
        });
        this.orderList = res.data;
      }
    },
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize;
      this.getOrderList();
    },
    handleCurrentChange(newPage) {
      this.queryInfo.pagenum = newPage;
      this.getOrderList();
    },
    showBox() {
      this.addressVisible = true;
    },
    handleClose() {
      this.$refs.addressFormRef.resetFields();
    },
    async showProgressBox() {
      const { data: res } = await this.$http.get(`/kuaidi/1106975712662`);
      if (res.meta.status != 200) {
        return this.$Message.error("获取物流信息失败");
      }
      console.log(res.data);
      this.progressInfo = res.data;
      this.showProgressVisible = true;
    },
  },
};
</script>

<style lang='less' scoped>
.el-cascader {
  width: 100%;
}
</style>
